//
//  HRRecommondViewModel.m
//  Hear
//
//  Created by mac on 2018/4/20.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRRecommondViewModel.h"
//#import "HRRecommendModel.h"
//#import "HRRecommendJsonModel.h"
#import "AssignToJsonModel.h"
@implementation HRRecommondViewModel

-(void)getRecommentContentParam:(HRRecommendRequestModel *)requestModel resultArr:(void(^)(NSArray *resultArr))resultArr
{
    NSString *url = @"";
    url = @"SoundInfo/Get_GetSoundHot";
    
    YTKApi *api = [YTKApi apiGetWithUrl:url parameter:requestModel];
    //    NSLog(@"开始请求==============%@",[self getCurrentTime]);
    
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            //            NSLog(@"拿到数据==============%@",[self getCurrentTime]);
            
            
            NSString *bodyContentStr = dict[@"BodyContent"];
            NSData *jsonData = [bodyContentStr dataUsingEncoding:NSUTF8StringEncoding];
            id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:NULL];
            NSArray *listArr = jsonObject[@"List"];
            NSMutableArray *mutableArray = [AssignToJsonModel Model:@"HRRecommendModel" ToArray:listArr];
            
            NSInteger totalCount = jsonObject[@"TotalCount"];
            NSMutableArray *layouts = [NSMutableArray array];
//            NSArray *conentArr = [NSArray modelArrayWithClass:[HRRecommendModel class] json:dict[@"BodyContent"]];
//                for (HRRecommendModel *model in mutableArray) {
//                    [layouts addObject:model];
//                }
            NSString *totalCountStr = [NSString stringWithFormat:@"%d",totalCount];
            NSArray *totalCountArr = [NSArray arrayWithObject:totalCountStr];
            [layouts addObject:totalCountArr];
            
            resultArr(layouts);
//            NSDictionary *contentDic = [NSDictionary modelDictionaryWithClass:[HRRecommendModel class] json:dict[@"BodyContent"]];
//            DDLog(@"contentDic is  %@",contentDic);
//            for (HRRecommendModel *model in conentArr) {
//                NSMutableArray *listSoundArr = [NSMutableArray array];
//                for (id ListSoundJeson  in model.List) {
//                    HRRecommendModel *listSoundmodel = [HRRecommendModel modelWithJSON:ListSoundJeson];
//                    [listSoundArr addObject:listSoundmodel];
//                }
//                model.listMutableArr = listSoundArr;
//            }
//
//            resultArr(conentArr);
            
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            DDLog(@"SoundInfo/Get_GetSounds_  failed");
            resultArr(nil);
        }];
    }];
}

-(void)getRecommondDataWithRequstDict:(NSMutableDictionary *)requstDict andUrl:(NSURL *)url andresultArray:(void (^)(NSMutableArray *))resultArray
{
    YTKApi *api = [YTKApi apiGetWithUrl:url para:requstDict];
    [api getSuccessToken:^{
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            [[NSUserDefaults standardUserDefaults] setInteger:300 forKey:@"cacheTime"];
            NSDictionary *dict = request.responseJSONObject;
            
            NSString *bodyContentStr = dict[@"BodyContent"];
            
//            NSArray *resultArr = [NSArray modelArrayWithClass:[HRRecommendModel class] json:dict[@"BodyContent"]];

            NSMutableArray *layouts = [NSMutableArray array];
            
//            for (HRRecommendModel *model in resultArr) {
//                
//                [layouts addObject:model];
//                
//            }
            
//            NSArray *resultArr = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:dict[@"BodyContent"]];
//            for (HRDynamicCommentModel *model in resultArr) {
//                model.ChildComments = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:model.ChildComments];
//            }
            
            
            resultArray(layouts);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            resultArray(nil);
            DDLog(@"url:%@ --------- is failure",url);
        }];
    }];
}

@end
